package com.betty.cannyimg;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends Activity {
    private Button btn_back,btn_canny,btn_gray;
    private ImageView iv;
    private SeekBar seekBar;
    private TextView tv;
    int threshold1,threshold2;

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
        System.loadLibrary("opencv_java3");
        System.loadLibrary("Canny");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView)findViewById(R.id.img);
        Bitmap img = BitmapFactory.decodeResource(getResources(),R.drawable.child);
        iv.setImageBitmap(img);

    }
    public void back(View view){
        Bitmap img1 = BitmapFactory.decodeResource(getResources(),R.drawable.child);
        iv.setImageBitmap(img1);
    }
    public void canny(View view){

        Bitmap img1 = ((BitmapDrawable) getResources().getDrawable(
                R.drawable.child)).getBitmap();
        int w = img1.getWidth();
        int h = img1.getHeight();
        int[] pix = new int[w*h];
        img1.getPixels(pix,0,w,0,0,w,h);
        seekbarView();
        int[] resultInt = new Canny().cannyImg(pix,w,h,threshold1,threshold2);
        Bitmap resultImg = Bitmap.createBitmap(w,h, Bitmap.Config.RGB_565);
        resultImg.setPixels(resultInt,0,w,0,0,w,h);
        iv.setImageBitmap(resultImg);
    }
    public void gray(View view){
        Bitmap img2 = ((BitmapDrawable) getResources().getDrawable(
                R.drawable.child)).getBitmap();
        int w = img2.getWidth();
        int h = img2.getHeight();
        int[] pix = new int[w*h];
        img2.getPixels(pix,0,w,0,0,w,h);
        int[] resultInt = new Canny().grayProc(pix,w,h);
        Bitmap resultImg = Bitmap.createBitmap(w,h, Bitmap.Config.RGB_565);
        resultImg.setPixels(resultInt,0,w,0,0,w,h);
        iv.setImageBitmap(resultImg);
    }
    public void seekbarView(){
        seekBar = (SeekBar)findViewById(R.id.seekbar);
        tv = (TextView)findViewById(R.id.tv);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv.setText("高低阈值："+progress+"、"+3*progress);
                threshold1 = progress;
                threshold2 = 3*progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }



            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

}
